package com.socool;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.Session;
import com.socool.manager.Manager_Shared;

public class Frag_Setting extends DSFragment implements OnClickListener {

	final static String LOG_TAG = "test";

	Button BT_logout;

	ImageView IV_push;
	ImageView IV_pushposition;

	ImageView IV_friend;
	ImageView IV_friendposition;
	TextView TV_friend;

	LinearLayout LL_coupon;
	LinearLayout LL_notice;
	LinearLayout LL_message;
	LinearLayout LL_blog;
	LinearLayout LL_faq;
	LinearLayout LL_help;
	LinearLayout LL_guide;

	TextView TV_count_message;
	TextView TV_count_notice;

	TextView TV_title;

	Manager_Shared mManagerShared;

	View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = (LinearLayout) inflater.inflate(R.layout.frag_setting,
				container, false);

		varInit();
		eventInit();

		return view;
	}

	public void onResume() {
		super.onResume();

		if (mManagerShared.getPref_GCMStatus().equals("TRUE")) {
			IV_push.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_pushposition
					.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_pushposition.setLayoutParams(localLayoutParams2);

		} else {
			IV_push.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_pushposition
					.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_pushposition.setLayoutParams(localLayoutParams1);

		}

		if (mManagerShared.getPref_RecommandStatus().equals("TRUE")) {
			IV_friend.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_friendposition
					.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_friendposition.setLayoutParams(localLayoutParams2);

		} else {
			IV_friend.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_friendposition
					.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_friendposition.setLayoutParams(localLayoutParams1);

		}

	}

	private void connectPush(boolean paramBoolean) {
		if (paramBoolean) {
			mManagerShared.setPref_GCMStatus("TRUE");
			IV_push.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_pushposition
					.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_pushposition.setLayoutParams(localLayoutParams2);
			TranslateAnimation localTranslateAnimation2 = new TranslateAnimation(
					1, -0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation2.setDuration(500L);
			IV_pushposition.startAnimation(localTranslateAnimation2);
		} else {
			mManagerShared.setPref_GCMStatus("FALSE");
			IV_push.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_pushposition
					.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_pushposition.setLayoutParams(localLayoutParams1);
			TranslateAnimation localTranslateAnimation1 = new TranslateAnimation(
					1, 0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation1.setDuration(500L);
			IV_pushposition.startAnimation(localTranslateAnimation1);
		}
	}

	private void connectRecommand(boolean paramBoolean) {
		if (paramBoolean) {
			mManagerShared.setPref_RecommandStatus("TRUE");
			IV_friend.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_friendposition
					.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_friendposition.setLayoutParams(localLayoutParams2);
			TranslateAnimation localTranslateAnimation2 = new TranslateAnimation(
					1, -0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation2.setDuration(500L);
			IV_friendposition.startAnimation(localTranslateAnimation2);
		} else {
			mManagerShared.setPref_RecommandStatus("FALSE");
			IV_friend.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_friendposition
					.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_friendposition.setLayoutParams(localLayoutParams1);
			TranslateAnimation localTranslateAnimation1 = new TranslateAnimation(
					1, 0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation1.setDuration(500L);
			IV_friendposition.startAnimation(localTranslateAnimation1);
		}
	}

	private void eventInit() {

		IV_push.setOnClickListener(this);
		IV_friend.setOnClickListener(this);
		LL_coupon.setOnClickListener(this);
		LL_notice.setOnClickListener(this);
		LL_message.setOnClickListener(this);
		LL_blog.setOnClickListener(this);
		LL_faq.setOnClickListener(this);
		LL_help.setOnClickListener(this);
		LL_guide.setOnClickListener(this);
		BT_logout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		if (v == IV_push) {
			connectPush(mManagerShared.getPref_GCMStatus().equals("TRUE") ? false
					: true);
		}
		else if (v == IV_friend) {
			connectRecommand(mManagerShared.getPref_RecommandStatus()
					.equals("TRUE") ? false : true);
		}
		else if (v == LL_coupon) {
		}
		else if (v == LL_notice) {
		}
		else if (v == LL_message) {
		}
		else if (v == LL_blog) {
		}
		else if (v == LL_faq) {
		}
		else if (v == LL_help) {
		}
		else if (v == LL_guide) {
		}
		else if (v == BT_logout) {
			onClickLogout();
			mManagerShared.setPref_Loginstatus("null");
			HomeActivity fca = (HomeActivity) getActivity();
			Intent intent = new Intent(fca, Act_Login.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(intent);
			fca.overridePendingTransition(R.anim.fade, R.anim.hold);
			fca.finish();

		}

	}

	private void varInit() {
		mManagerShared = new Manager_Shared(getActivity());

		LL_faq = ((LinearLayout) view.findViewById(R.id.LL_faq));
		LL_notice = ((LinearLayout) view.findViewById(R.id.LL_notice));
		LL_blog = ((LinearLayout) view.findViewById(R.id.LL_blog));

		LL_message = ((LinearLayout) view.findViewById(R.id.LL_message));
		LL_guide = ((LinearLayout) view.findViewById(R.id.LL_guide));
		LL_help = ((LinearLayout) view.findViewById(R.id.LL_help));
		LL_coupon = ((LinearLayout) view.findViewById(R.id.LL_coupon));

		TV_count_notice = ((TextView) view.findViewById(R.id.TV_count_notice));
		TV_count_message = ((TextView) view.findViewById(R.id.TV_count_message));

		IV_push = ((ImageView) view.findViewById(R.id.IV_push));
		IV_pushposition = ((ImageView) view.findViewById(R.id.IV_pushposition));

		IV_friend = ((ImageView) view.findViewById(R.id.IV_friend));
		IV_friendposition = ((ImageView) view
				.findViewById(R.id.IV_friendposition));

		TV_friend = (TextView) view.findViewById(R.id.TV_friend);
		TV_friend.post(new Runnable() {
			@Override
			public void run() {

				String str = getResources().getString(
						R.string.act_setting_friend_description);
				String text = breakText(TV_friend.getPaint(), str,
						TV_friend.getWidth());
				TV_friend.setText(text);
			}
		});

		BT_logout = ((Button) view.findViewById(R.id.BT_logout));

	}

	public String breakText(Paint textPaint, String strText, int breakWidth) {
		StringBuilder sb = new StringBuilder();
		int endValue = 0;
		do {
			endValue = textPaint.breakText(strText, true, breakWidth, null);
			if (endValue > 0) {
				sb.append(strText.substring(0, endValue)).append("\n");
				strText = strText.substring(endValue);
			}
		} while (endValue > 0);
		return sb.toString().substring(0, sb.length() - 1);
	}

	private void onClickLogout() {
		Session session = Session.getActiveSession();

		if (session != null && !session.isClosed()) {
			session.closeAndClearTokenInformation();
			Toast.makeText(getActivity(), "Logout!!", Toast.LENGTH_SHORT)
					.show();
		} else {
			Log.d(LOG_TAG, "session is null");
		}
	}

}
